package net.ufrog.common.redis;

import net.ufrog.common.Logger;
import net.ufrog.common.cache.CacheKey;
import net.ufrog.common.cache.Caches;
import net.ufrog.common.cache.SimpleCacheKey;
import net.ufrog.common.redis.beans.User;
import org.junit.Assert;
import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;

/**
 * @author ultrafrog, ufrog.net@gmail.com
 * @version 0.1, 2016-09-26
 * @since 0.1
 */
public class CodisTester {

    private static List<User> lUser;
    private static CodisImpl codisImpl = new CodisImpl(new SimpleCacheKey("codis"));

    @Test
    public void testSingleCache() {
        User user = new User("你好", "男", 30, "account", "我也不知道的地址");
        codisImpl.set(user.getId(), user, 7200);

        User target = (User) codisImpl.get(user.getId());
        Logger.info(">>>>>>>>> %s", target.getId());
        Logger.info(">>>>>>>>> %s", target.getName());
        Logger.info(">>>>>>>>> %s", target.getGender());
        Logger.info(">>>>>>>>> %s", target.getAge());
        Logger.info(">>>>>>>>> %s", target.getAccount());
        Logger.info(">>>>>>>>> %s", target.getAddress());
        Assert.assertEquals(user.getId(), target.getId());
    }

    //@Test
    public void testLoopCache() {
        lUser = new ArrayList<>();
        IntStream.range(0, 1000).forEach(idx -> {
            User user = new User("name" + idx, (idx % 2 == 1 ? "男" : "女"), idx, "account" + idx, "门牌号" + idx);
            codisImpl.add(user.getId(), user, 7200);
            lUser.add(user);
        });

        lUser.forEach(user -> {
            Logger.info("%s >>>>> %s: %s", user.getId(), user.getName(), user.getGender());
            User target = (User) codisImpl.get(user.getId());
            Assert.assertEquals(user.getId(), target.getId());
        });
    }

    //@Test
    public void testUnlimitCache() {
        try (Jedis jedis = codisImpl.getJedis()) {
            for (int i = 0; i < 100; i++) {
                jedis.set("idx_" + i, "value_" + i);
            }
        }
    }

    @Test
    public void testIncr() {
        try (Jedis jedis = codisImpl.getJedis()) {
            jedis.set("incr_test", "0");
            IntStream.range(0, 100).forEach(idx -> jedis.incr("incr_test"));
            Assert.assertEquals("100", jedis.get("incr_test"));
        }
    }

    @Test
    public void testCaches() {
        User user = new User("你好_caches", "男", 30, "account_caches", "我也不知道的地址_caches");
        CacheKey cacheKey = new SimpleCacheKey("codis");
        Caches.set("test_codis_impl", user, Caches.getImpl(cacheKey).getDuration(), cacheKey);

        User target = Caches.get("test_codis_impl", User.class);
        Logger.info(">>>>>>>>> %s", target.getId());
        Logger.info(">>>>>>>>> %s", target.getName());
        Logger.info(">>>>>>>>> %s", target.getGender());
        Logger.info(">>>>>>>>> %s", target.getAge());
        Logger.info(">>>>>>>>> %s", target.getAccount());
        Logger.info(">>>>>>>>> %s", target.getAddress());
        Assert.assertEquals(user.getId(), target.getId());
    }
}
